package com.example.shareapp.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shareapp.R;
import com.example.shareapp.adapter.ListAdapter;
import com.example.shareapp.api.UriApi;
import com.example.shareapp.bean.AllDataEntry;
import com.example.shareapp.listeners.OnclickListeners;
import com.example.shareapp.utils.DialogUtils;
import com.example.shareapp.utils.PlatformUtil;
import com.example.shareapp.view.LocalImageLoader;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class MoreActivity extends Activity implements View.OnClickListener {
    private Context mContext;

    private Banner mBanner;//初始化Bnner控件使用
    private RecyclerView recyclerView;
    private int mWidth;
    private ListAdapter listAdapter;
    private AllDataEntry mAllDataEntry;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        mContext = this;
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        mWidth = metric.widthPixels;     // 屏幕宽度（像素）
        mAllDataEntry = (AllDataEntry)getIntent().getSerializableExtra("data");
        initView();
        initClick();
        initBanner();
    }


    private void initView() {
        mBanner = findViewById(R.id.banner_container);
        recyclerView = findViewById(R.id.rv_list);
        //创建线性布局
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        //垂直方向
        mLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);
        listAdapter = new ListAdapter(mContext,mAllDataEntry.liebiaosList);
        recyclerView.setAdapter(listAdapter);
    }

    private void initClick() {
    }



    private LocalImageLoader mImageLoader;//加载图片的工具类
    private ArrayList<String> imagePath;//存放图片的网络地址
    private ArrayList<String> imageTitle;//存放图片的title
    private void initBanner() {
        imagePath = new ArrayList<>();
        imageTitle = new ArrayList<>();
        imagePath.add(UriApi.URI_API+mAllDataEntry.aBanner.path);
        imageTitle.add("测试1");
        mImageLoader = new LocalImageLoader();
        //样式CIRCLE_INDICATOR_TITLE 标题和小圆点同时出现
        // CIRCLE_INDICATOR_TITLE_INSIDE 小圆点和标题在同一栏
        // CIRCLE_INDICATOR：无标题，有小圆点
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //加载器
        mBanner.setImageLoader(mImageLoader);
        //动画效果
        mBanner.setBannerAnimation(Transformer.ZoomOutSlide);
        //图片标题
        mBanner.setBannerTitles(imageTitle);
        //间隔时间
        mBanner.setDelayTime(4500);
        //是否为自动轮播
        mBanner.isAutoPlay(false);
        //图片小点显示所在位置
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //图片加载地址
        mBanner.setImages(imagePath);
        //启动轮播图。
        mBanner.start();
        //监听轮播图
        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

                if (TextUtils.isEmpty(mAllDataEntry.aBanner.url)){
                    return;
                }

                Intent intent= new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(mAllDataEntry.aBanner.url);
                intent.setData(content_url);
                startActivity(intent);

            }
        });
    }









    //权限数组（申请定位）
    private String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};

    //返回code
    private static final int OPEN_SET_REQUEST_CODE = 100;

    //调用此方法判断是否拥有权限
    private void initPermissions() {

        if (lacksPermission()) {//判断是否拥有权限
            //请求权限，第二参数权限String数据，第三个参数是请求码便于在onRequestPermissionsResult 方法中根据code进行判断
            ActivityCompat.requestPermissions(this, permissions, OPEN_SET_REQUEST_CODE);
        } else {
            //拥有权限执行操作
        }
    }

    //如果返回true表示缺少权限
    public boolean lacksPermission() {
        for (String permission : permissions) {
            //判断是否缺少权限，true=缺少权限
            if(ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED){
                return true;
            }
        }
        return false;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode){//响应Code
            case OPEN_SET_REQUEST_CODE:
                if (grantResults.length > 0) {
                    for(int i = 0; i < grantResults.length; i++){
                        if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
                            Toast.makeText(mContext,"未拥有相应权限",Toast.LENGTH_LONG).show();
                            return;
                        }
                    }
                    //拥有权限执行操作
                } else {
                    Toast.makeText(mContext,"未拥有相应权限",Toast.LENGTH_LONG).show();
                }
                break;
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
        }
    }



}
